home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / SB2DB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.9 KB  |  298 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994, 1996. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40.  
  41. /* sb2db.c - This program demonstrates switching between single buffered
  42.    and double buffered windows when using GLUT.  Use the pop-up menu to
  43.    change the buffering style used.  On machine that split the screen's
  44.    color resolution in half when double buffering, you should notice better
  45.    coloration (less or no dithering) in single buffer mode (but flicker on
  46.    redraws, particularly when rotation is toggled on).  */
  47.  
  48. /* This program is based on the GLUT scene.c program. */
  49.  
  50. #include <stdlib.h>
  51. #include <GL/glut.h>
  52.  
  53. int sbwin, dbwin;
  54. int angle;
  55.  
  56. /*  Initialize material property and light source.  */
  57. void
  58. myinit(void)
  59. {
  60.   GLfloat light_ambient[] =
  61.   {0.3, 0.3, 0.3, 1.0};
  62.   GLfloat light_diffuse[] =
  63.   {6.0, 6.0, 6.0, 1.0};
  64.   GLfloat light_specular[] =
  65.   {1.0, 1.0, 1.0, 1.0};
  66.   /* light_position is NOT default value */
  67.   GLfloat light_position[] =
  68.   {-1.0, 1.0, 1.0, 0.0};
  69.  
  70.   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  71.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  72.   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  73.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  74.  
  75.   glEnable(GL_LIGHT0);
  76.   glDepthFunc(GL_LESS);
  77.   glEnable(GL_DEPTH_TEST);
  78.   glEnable(GL_LIGHTING);
  79. }
  80.  
  81. void
  82. display(void)
  83. {
  84.   static GLfloat red[] =
  85.   {0.8, 0.0, 0.0, 1.0};
  86.   static GLfloat yellow[] =
  87.   {0.8, 0.8, 0.0, 1.0};
  88.   static GLfloat green[] =
  89.   {0.0, 0.8, 0.0, 1.0};
  90.  
  91.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92.  
  93.   glPushMatrix();
  94.   glRotatef(angle, 1.0, 0.0, 0.0);
  95.   glScalef(1.3, 1.3, 1.3);
  96.   glRotatef(20.0, 1.0, 0.0, 0.0);
  97.  
  98.   glPushMatrix();
  99.   glTranslatef(-0.75, 0.5, 0.0);
  100.   glRotatef(90.0, 1.0, 0.0, 0.0);
  101.   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
  102.   glutSolidTorus(0.275, 0.85, 10, 15);
  103.   glPopMatrix();
  104.  
  105.   glPushMatrix();
  106.   glTranslatef(-0.75, -0.5, 0.0);
  107.   glRotatef(270.0, 1.0, 0.0, 0.0);
  108.   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, yellow);
  109.   glutSolidCone(1.0, 1.0, 40, 40);
  110.   glPopMatrix();
  111.  
  112.   glPushMatrix();
  113.   glTranslatef(0.75, 0.0, -1.0);
  114.   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, green);
  115.   glutSolidIcosahedron();
  116.   glPopMatrix();
  117.  
  118.   glPopMatrix();
  119.  
  120.   if (glutGetWindow() == sbwin) {
  121.     glFlush();
  122.   } else {
  123.     glutSwapBuffers();
  124.   }
  125. }
  126.  
  127. /* Used by both windows, this routine setups the OpenGL context's
  128.    projection matrix correctly.  Note that we call this routine for
  129.    both contexts to keep them in sync after reshapes. */
  130. void
  131. reshapeOpenGLState(int w, int h)
  132. {
  133.   glMatrixMode(GL_PROJECTION);
  134.   glLoadIdentity();
  135.   if (w <= h)
  136.     glOrtho(-2.5, 2.5, -2.5 * (GLfloat) h / (GLfloat) w,
  137.       2.5 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
  138.   else
  139.     glOrtho(-2.5 * (GLfloat) w / (GLfloat) h,
  140.       2.5 * (GLfloat) w / (GLfloat) h, -2.5, 2.5, -10.0, 10.0);
  141.   glMatrixMode(GL_MODELVIEW);
  142. }
  143.  
  144. /* When the single buffered (ie, the top window) gets resized, we
  145.    need to resize the child double buffered window as well.  Hence
  146.    the glutReshapeWindow on the child.  NOTE:  You want a separate
  147.    resize callback for the double buffered window to set the viewport
  148.    since the window's size won't really be changed until the double buffered
  149.    gets its dbReshape callback.  Otherwise, you could trick OpenGL intop
  150.    clipping based on the old window size. */
  151. void
  152. sbReshape(int w, int h)
  153. {
  154.   glutSetWindow(sbwin);
  155.   glViewport(0, 0, w, h);
  156.   reshapeOpenGLState(w, h);
  157.   glutSetWindow(dbwin);
  158.   glutReshapeWindow(w, h);
  159. }
  160.  
  161. void
  162. dbReshape(int w, int h)
  163. {
  164.   glViewport(0, 0, w, h);
  165.   reshapeOpenGLState(w, h);
  166. }
  167.  
  168. void
  169. rotation(void)
  170. {
  171.   angle += 2;
  172.   angle = angle % 360;
  173.   glutPostRedisplay();
  174. }
  175.  
  176. int animation = 0;  /* Are we doing an animated rotation currently? */
  177.  
  178. void
  179. main_menu(int value)
  180. {
  181.   switch (value) {
  182.   case 1:
  183.     /* Smart toggle rotation ensures we switch to double buffered when
  184.        animating and single buffered when not animating. */
  185.     animation = 1 - animation;  /* Toggle. */
  186.     if (animation) {
  187.       glutIdleFunc(rotation);
  188.       glutSetWindow(sbwin);
  189.       glutSetWindowTitle("sb2db - double buffer mode");
  190.       glutSetWindow(dbwin);
  191.       glutShowWindow();   /* Show the double buffered window. */
  192.     } else {
  193.       glutIdleFunc(NULL);
  194.       glutSetWindow(sbwin);
  195.       glutSetWindowTitle("sb2db - single buffer mode");
  196.       glutSetWindow(dbwin);
  197.       glutHideWindow();   /* Hide the double buffered window. */
  198.     }
  199.     break;
  200.   case 2:
  201.     glutSetWindow(dbwin);
  202.     glutHideWindow();   /* Hide the double buffered window. */
  203.     glutSetWindow(sbwin);
  204.     glutSetWindowTitle("sb2db - single buffer mode");
  205.     break;
  206.   case 3:
  207.     glutSetWindow(sbwin);
  208.     glutSetWindowTitle("sb2db - double buffer mode");
  209.     glutSetWindow(dbwin);
  210.     glutShowWindow();   /* Show the double buffered window. */
  211.     break;
  212.   case 4:
  213.     animation = 1 - animation;  /* Toggle. */
  214.     if (animation)
  215.       glutIdleFunc(rotation);
  216.     else
  217.       glutIdleFunc(NULL);
  218.     break;
  219.   case 666:
  220.     exit(0);
  221.     break;
  222.   }
  223. }
  224.  
  225. /* You have to track the visibility of both the single buffered
  226.    and double buffered windows together. */
  227. void
  228. visibility(int state)
  229. {
  230.   static int sbvis = GLUT_NOT_VISIBLE, dbvis = GLUT_NOT_VISIBLE;
  231.   int eithervis;
  232.  
  233.   if (glutGetWindow() == sbwin) {
  234.     sbvis = state;
  235.   } else {
  236.     dbvis = state;
  237.   }
  238.   eithervis = (sbvis == GLUT_VISIBLE) || (dbvis == GLUT_VISIBLE);
  239.   if (eithervis) {
  240.     /* Resume rotating idle callback if we become visible and
  241.        animation is enabled. */
  242.     if (animation) {
  243.       glutIdleFunc(rotation);
  244.     }
  245.   } else {
  246.     /* Disable animation when both windows are not visible. */
  247.     glutIdleFunc(NULL);
  248.   }
  249. }
  250.  
  251. int
  252. main(int argc, char **argv)
  253. {
  254.   glutInitWindowSize(500, 500);
  255.   glutInit(&argc, argv);
  256.   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SINGLE);
  257.  
  258.   /* The top window is single buffered. */
  259.   sbwin = glutCreateWindow(argv[0]);
  260.   glutReshapeFunc(sbReshape);
  261.   glutDisplayFunc(display);
  262.   glutVisibilityFunc(visibility);
  263.   myinit();
  264.  
  265.   /* The child window is double buffered.  We show this window
  266.      when displaying double buffered and hide it to show the
  267.      single buffered window. */
  268.   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  269.   dbwin = glutCreateSubWindow(glutGetWindow(),
  270.     0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  271.   glutDisplayFunc(display);
  272.   glutReshapeFunc(dbReshape);
  273.   glutVisibilityFunc(visibility);
  274.  
  275.   /* Call myinit for both the single buffered window and the
  276.      double buffered window.  We must mirror the same OpenGL
  277.      state in both window's OpenGL contexts.  If you make this
  278.      program more complicated, remember to keep the window's
  279.      context state in sync. */
  280.   myinit();
  281.  
  282.   /* Initially hide the double buffered window to start in
  283.      single buffered mode. */
  284.   glutHideWindow();
  285.  
  286.   glutCreateMenu(main_menu);
  287.   glutAddMenuEntry("Smart rotation toggle", 1);
  288.   glutAddMenuEntry("Single buffer", 2);
  289.   glutAddMenuEntry("Double buffer", 3);
  290.   glutAddMenuEntry("Toggle rotation", 4);
  291.   glutAddMenuEntry("Quit", 666);
  292.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  293.   glutSetWindow(sbwin);
  294.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  295.   glutMainLoop();
  296.   return 0;             /* ANSI C requires main to return int. */
  297. }
  298.